﻿using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WCS.Entity;
using WCS.Helpers;
using WCS.Helpers.ExportExcel;
using WCS.Utils;
using static System.Collections.Specialized.BitVector32;

namespace WCS.Controls
{
    public partial class CallMaterial : UIPage
    {
        public CallMaterial()
        {
            InitializeComponent();
        }
        public static string connStrings = System.Configuration.ConfigurationManager.AppSettings["SQLConn"].ToString();

        Dictionary<string, string> _MaterialType = new Dictionary<string, string>();
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SortingTask_Load(object sender, EventArgs e)
        {
            //MawbNoBind();
            this.uiPagination1.ActivePage = 1;

            var type = _SqlSugarDapper.DB.Queryable<WcsDevPlc>().Where(w => w.PLC_Name.Contains("托盘库堆垛机"))
                   .First().Plc_Type;
            if (type == "1")
            {
                uiSwitch1.Active = true;
            }
            else
            {
                uiSwitch1.Active = false;
            }
        }

        /// <summary>
        /// 数据库对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 主单绑定
        /// </summary>
        private void MawbNoBind()
        {
            var _material = _SqlSugarDapper.DB.Queryable<PalletToMaterial>().ToList();

            for (int i = 0; i < _material.Count; i++)
            {
                _MaterialType.Add(_material[i].MaterialType, _material[i].MaterialType);
            }

            MaterialTypeComboBox.DataSource = new BindingSource(_MaterialType, null);
            MaterialTypeComboBox.DisplayMember = "key";
            MaterialTypeComboBox.ValueMember = "value";


        }

        /// <summary>
        /// 成绩绑定
        /// </summary>
        private void TaskTableBind()
        {
            //获取当前页面的index
            int index = this.uiPagination1.ActivePage - 1;
            int pageIndex = this.uiPagination1.ActivePage;
            int size = this.uiPagination1.PageSize;
            int begin = (index * size) + 1;
            int end = (index + 1) * size;
            int count = 0;
            this.dgvScoreList.DataSource = "SELECT CallStation,isSupply,MaterialType,SupplyNum ,CASE CallStation WHEN '3001' THEN 'D1'WHEN '3002' THEN 'D2' WHEN '3003' THEN 'D3' WHEN '3004' THEN 'D4' WHEN '3005' THEN 'D5' WHEN '3006' THEN 'C1'WHEN '3007' THEN 'C2' WHEN '3008' THEN 'C3' WHEN '3009' THEN 'C4' WHEN '3010' THEN 'C5' ELSE 'null' END FROM WCS_CallMaterial WHERE 1=1".QueryStrCount(out count);

            //this.dgvScoreList.DataSource = _SqlSugarDapper.DB.Queryable<WCS.Entity.CallMaterial>()
            //    .OrderBy("CallStation")//然后根据创建时间排序
            //    .ToPageList(pageIndex, size, ref count);

            //uiPagination1.TotalCount = count;

            this.materialCount.Text = "0";
            var type = _SqlSugarDapper.DB.Queryable<WcsDevPlc>().Where(w => w.PLC_Name.Contains("托盘库堆垛机"))
                   .First().Plc_Type;
            if (type == "1")
            {
                uiSwitch1.Active = true;
            }
            else
            {
                uiSwitch1.Active = false;
            }
        }

        private void CallMaterialQuery()
        {
            //获取当前页面的index
            int index = this.uiPagination1.ActivePage - 1;
            int pageIndex = this.uiPagination1.ActivePage;
            int size = this.uiPagination1.PageSize;
            int begin = (index * size) + 1;
            int end = (index + 1) * size;
            int count = 0;
            this.dgvScoreList.DataSource = $"SELECT CallStation,isSupply,MaterialType,SupplyNum ,CASE CallStation WHEN '3001' THEN 'D1'WHEN '3002' THEN 'D2' WHEN '3003' THEN 'D3' WHEN '3004' THEN 'D4' WHEN '3005' THEN 'D5' WHEN '3006' THEN 'C1'WHEN '3007' THEN 'C2' WHEN '3008' THEN 'C3' WHEN '3009' THEN 'C4' WHEN '3010' THEN 'C5' ELSE 'null' END FROM WCS_CallMaterial WHERE MaterialType='{MaterialTypeComboBox.Text}'".QueryStrCount(out count);

            materialCount.Text = _SqlSugarDapper.DB.Queryable<WCS.Entity.CallMaterial>()
                .Where(w => w.MaterialType == MaterialTypeComboBox.Text)
                .ToList()
                .Count().ToString();
            //this.dgvScoreList.DataSource = _SqlSugarDapper.DB.Queryable<WCS.Entity.CallMaterial>()
            //    .OrderBy("CallStation")//然后根据创建时间排序
            //    .ToPageList(pageIndex, size, ref count);

            //uiPagination1.TotalCount = count;
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFind_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void queryCallMaterial_Click(object sender, EventArgs e)
        {
            TaskTableBind();
        }

        /// <summary>
        /// 页面改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            TaskTableBind();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            CallMaterialQuery();
        }

        //删除
        private void uiButton2_Click(object sender, EventArgs e)
        {
            var dataselect = this.dgvScoreList.SelectedRows;
            //判断是否选中数据
            if (dataselect.Count > 0)
            {
                //第一行colunm命名为code的值
                string materialType = Convert.ToString(dataselect[0].Cells[1].Value);
                //第一行colunm命名为name的值
                string station = Convert.ToString(dataselect[0].Cells[4].Value);
                string stationtp = Convert.ToString(dataselect[0].Cells[0].Value);

                DialogResult res = MessageBox.Show($"确定要删除{stationtp}的铸造任务？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    _SqlSugarDapper.DB.Ado.ExecuteCommand($"DELETE top(1) WCS_CallMaterial where CallStation = '{station}' and MaterialType = '{materialType}'");

                    MessageBox.Show($"删除铸造任务{stationtp}完成");
                    TaskTableBind();
                }
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show($"确定要删除所有铸造叫料任务吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res == DialogResult.OK)
            {
                _SqlSugarDapper.DB.Ado.ExecuteCommand($"DELETE WCS_CallMaterial");

                MessageBox.Show($"删除所有铸造叫料任务成功");
                TaskTableBind();
            }
        }

        /// <summary>
        /// 铸造任务开启关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (uiSwitch1.Active == true)
            {
                DialogResult res = MessageBox.Show($"确定要关闭铸造任务？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    _SqlSugarDapper.DB.Updateable<WcsDevPlc>()
                                   .SetColumns(w => w.Plc_Type == "1")
                                   .Where(w => w.PLC_Name.Contains("托盘库堆垛机"))
                                   .ExecuteCommand();
                }
            }
            else
            {
                DialogResult res = MessageBox.Show($"确定要开启铸造任务？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    _SqlSugarDapper.DB.Updateable<WcsDevPlc>()
                                 .SetColumns(w => w.Plc_Type == "0")
                                 .Where(w => w.PLC_Name.Contains("托盘库堆垛机"))
                                 .ExecuteCommand();
                }
            }
        }

        private void MaterialTypeComboBox_DropDown(object sender, EventArgs e)
        {
            var list = _SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>()
             .ToList()
             .Where(w => !w.MaterialType.Contains("物料"))
             .OrderBy(w => w.MaterialType)
             .GroupBy(w => w.MaterialType)
             .ToList();

            MaterialTypeComboBox.Items.Clear();
            foreach (var item in list)
            {
                MaterialTypeComboBox.Items.Add(item.Key);
            }
        }
    }
}
